package dao

import (
	"context"
	"errors"
	"fmt"
	_ "gorm.io/driver/mysql"
	"gorm.io/gorm"
)

type User struct {
	gorm.Model
	Username string
	Password string
}

// gorm.Model 的定义
func (d *dao) initmysql() {
	fmt.Println("init test")
	d.db.Migrator().CreateTable(&User{})

}

// 设置表名
func (u User) TableName() string {

	return "User"
}
func (d *dao) InsertUser(c context.Context, username, password string) (userid uint, err error) {
	var user User

	d.db.Where("Username=?", username).Find(&user)

	if user.Username != "" {
		err = errors.New("insert already exist")
		userid = user.ID
		return
	}

	user = User{Username: username, Password: password}
	d.db.Create(&user)

	userid = user.ID

	return
}
func (d *dao) Close() {

}
